package org.xxpay.service.impl;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xxpay.core.entity.StatisticsPayPassage;
import org.xxpay.core.entity.StatisticsPayPassageExample;
import org.xxpay.core.service.IStatisticsPayPassageService;
import org.xxpay.service.dao.mapper.StatisticsPayPassageMapper;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 支付通道统计表 服务实现类
 * </p>
 *
 * @author zhuxiao
 * @since 2021-02-26
 */
@Service
public class StatisticsPayPassageServiceImpl implements IStatisticsPayPassageService {

    @Autowired
    private StatisticsPayPassageMapper statisticsPayPassageMapper;

    @Override
    public List<StatisticsPayPassage> select(int pageIndex, int pageSize, StatisticsPayPassage statisticsPayPassage, JSONObject queryObj) {
        StatisticsPayPassageExample example = new StatisticsPayPassageExample();
        example.setOrderByClause("statisticsDate desc");
        example.setOffset(pageIndex);
        example.setLimit(pageSize);
        StatisticsPayPassageExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, statisticsPayPassage, queryObj);
        return statisticsPayPassageMapper.selectByExample(example);
    }

    @Override
    public long count(StatisticsPayPassage statisticsPayPassage, JSONObject queryObj) {
        StatisticsPayPassageExample example = new StatisticsPayPassageExample();
        StatisticsPayPassageExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, statisticsPayPassage, queryObj);
        return statisticsPayPassageMapper.countByExample(example);
    }

    @Override
    public long count4PayPassage(Date createTimeStart, Date createTimeEnd) {
        Map param = new HashMap<>();
        if(createTimeStart != null) param.put("createTimeStart", createTimeStart);
        if(createTimeEnd != null) param.put("createTimeEnd", createTimeEnd);
        return statisticsPayPassageMapper.count4PayPassage(param);
    }

    @Override
    public int add(StatisticsPayPassage statisticsPayPassage) {
        return statisticsPayPassageMapper.insertSelective(statisticsPayPassage);
    }

    @Override
    public int updateByPrimaryKey(StatisticsPayPassage statisticsPayPassage) {
        return statisticsPayPassageMapper.updateByPrimaryKeySelective(statisticsPayPassage);
    }

    @Override
    public StatisticsPayPassage findByPrimaryKey(Integer payPassageId, Integer payPassageAccountId, Date statisticsDate) {
        return statisticsPayPassageMapper.selectByPrimaryKey(payPassageId, payPassageAccountId, statisticsDate);
    }

    @Override
    public List<Map> countList4PayPassage(int offset, int limit, Date createTimeStart, Date createTimeEnd) {
        Map param = new HashMap<>();
        param.put("offset",offset);
        param.put("limit",limit);
        if(createTimeStart != null) param.put("createTimeStart", createTimeStart);
        if(createTimeEnd != null) param.put("createTimeEnd", createTimeEnd);
        return statisticsPayPassageMapper.countList4PayPassage(param);
    }

    @Override
    public long count4PayPassageAccount(Integer payPassageId, Date createTimeStart, Date createTimeEnd) {
        Map param = new HashMap<>();
        if(payPassageId != null) param.put("payPassageId", payPassageId);
        if(createTimeStart != null) param.put("createTimeStart", createTimeStart);
        if(createTimeEnd != null) param.put("createTimeEnd", createTimeEnd);
        return statisticsPayPassageMapper.count4PayPassageAccount(param);
    }

    @Override
    public List<Map> countList4PayPassageAccount(int offset, int limit, Date createTimeStart, Date createTimeEnd, Integer payPassageId) {
        Map param = new HashMap<>();
        param.put("offset",offset);
        param.put("limit",limit);
        if(payPassageId != null) param.put("payPassageId", payPassageId);
        if(createTimeStart != null) param.put("createTimeStart", createTimeStart);
        if(createTimeEnd != null) param.put("createTimeEnd", createTimeEnd);
        return statisticsPayPassageMapper.countList4PayPassageAccount(param);
    }

    @Override
    public int updateStatistics(StatisticsPayPassage updateRecord) {
        return statisticsPayPassageMapper.updateStatistics(updateRecord);
    }


    void setCriteria(StatisticsPayPassageExample.Criteria criteria, StatisticsPayPassage statisticsPayPassage, JSONObject queryObj) {
        if(statisticsPayPassage != null) {
            if(statisticsPayPassage.getPayPassageId() != null) criteria.andPayPassageIdEqualTo(statisticsPayPassage.getPayPassageId());
            if(statisticsPayPassage.getPayPassageAccountId() != null) criteria.andPayPassageAccountIdEqualTo(statisticsPayPassage.getPayPassageAccountId());
            if(statisticsPayPassage.getStatisticsDate() != null) criteria.andStatisticsDateEqualTo(statisticsPayPassage.getStatisticsDate());
        }
        if(queryObj != null) {
            if(queryObj.getDate("createTimeStart") != null) criteria.andCreateTimeGreaterThanOrEqualTo(queryObj.getDate("createTimeStart"));
            if(queryObj.getDate("createTimeEnd") != null) criteria.andCreateTimeLessThanOrEqualTo(queryObj.getDate("createTimeEnd"));
        }
    }

}
